package gui;

import net.BackClient;
import util.Parser;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class RegisterUI extends JFrame {
    private static final long serialVersionUID = 6758891080960583715L;
    BackClient backClient = new BackClient();
    Parser parser = new Parser();
    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;
    private JPasswordField passwordField_1;

    public RegisterUI() {
        setTitle("注册");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 479, 277);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblpp = new JLabel("欢迎注册");
        lblpp.setFont(new Font("宋体", Font.PLAIN, 17));
        lblpp.setBounds(163, 29, 145, 32);
        contentPane.add(lblpp);

        JLabel label = new JLabel("用户名：");
        label.setFont(new Font("宋体", Font.PLAIN, 17));
        label.setBounds(63, 72, 76, 15);
        contentPane.add(label);

        JLabel label_1 = new JLabel("密码：");
        label_1.setFont(new Font("宋体", Font.PLAIN, 17));
        label_1.setBounds(63, 105, 76, 15);
        contentPane.add(label_1);

        JLabel label_2 = new JLabel("确认密码：");
        label_2.setFont(new Font("宋体", Font.PLAIN, 17));
        label_2.setBounds(63, 144, 177, 15);
        contentPane.add(label_2);

        textField = new JTextField();
        textField.setBounds(221, 71, 112, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(221, 104, 112, 21);
        contentPane.add(passwordField);

        passwordField_1 = new JPasswordField();
        passwordField_1.setBounds(221, 143, 112, 21);
        contentPane.add(passwordField_1);

        JButton button = new JButton("确定");
        button.addActionListener(arg0 -> {
            String password1 = String.valueOf(passwordField.getPassword());
            String password2 = String.valueOf(passwordField_1.getPassword());

            if (password1.equals(password2)) {
                String backMessage = backClient.receiveMassage();

                boolean isSuccess = parser.isRegister(backMessage);
                if (isSuccess) {
                    JOptionPane.showMessageDialog(null, "注册成功");
                    LogInUI inUI = new LogInUI();
                    inUI.setVisible(true);
                    RegisterUI.this.dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "注册失败");
                    textField.setText("");
                }
            } else {
                JOptionPane.showMessageDialog(null, "请输入一致的密码");
                passwordField.setText("");
                passwordField_1.setText("");
            }

        });
        button.setBounds(360, 190, 93, 23);
        contentPane.add(button);

        JButton button_1 = new JButton("返回");
        button_1.addActionListener(arg0 -> {
            RegisterUI.this.setVisible(false);
            LogInUI inUI = new LogInUI();
            inUI.setVisible(true);
        });
        button_1.setBounds(10, 190, 93, 23);
        contentPane.add(button_1);
    }
}
